<?php

namespace App\Api\Controllers;

use App\Api\Models\DriverModel;
use App\Http\Controllers\Api\BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class DriverController extends BaseController
{
    /**
     * 列表
     * change 在 2021/10/25 15:37 创建
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'pageSize' =>'required|integer',//页面大小
            'currentPage' =>'required|integer',//当前页
        ], [
            'pageSize.required' => '参数`pageSize`丢失',
            'pageSize.integer' => '参数`pageSize`仅能为数字',
            'currentPage.required' => '参数`currentPage`丢失',
            'currentPage.integer' => '参数`currentPage`仅能为数字',
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        //查询
        $model = DriverModel::where('main_id',$this->getMainUserId())->orderBy('id','desc')->simplePaginate($data['pageSize'],['*'], '',$data['currentPage']);
        return $this->success($model);
    }

    /**
     * 详情
     * change 在 2021/12/14 09:57 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function find(Request $request){
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' =>'required|integer',//企业类型主键id
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        $model = DriverModel::find($data['id']);
        return $this->success($model);
    }
    /**
     * 下拉框
     * change 在 2021/10/25 15:37 创建
     * @return \Illuminate\Http\JsonResponse
     */
    public function select(Request $request){
        $model = DriverModel::where('main_id',$this->getMainUserId())->get();
        return $this->success($model);
    }
    /**
     * 添加
     * change 在 2021/10/25 15:37 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request){
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'plate_number' =>'required|string',//车牌
            'name' =>'required|string',//司机姓名
//            'logistics_company' =>'required|string',//物流公司
//            'remark' =>'required|string',//备注
//            'plate_image' =>'required|string',//车牌照片
//            'id_card_image' =>'required|string',//身份证照片
//            'driving_licence' =>'required|string',//驾驶证照片
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        $messages = [
            'phone.required' => '手机号不能为空，请输入手机号',
            'phone.regex' => '手机号格式错误，请重试',
        ];

        $vali = Validator::make($data, [
            'phone' => 'required|regex:/^1[3456789][0-9]{9}$/',
        ], $messages);

        //参数错误响应
        if ($infos = $vali->errors()->first()) {
            return $this->fail('', $infos);
        }

        $data['main_id'] = $this->getMainUserId();
        $data['created_at'] = time();
        $id = DriverModel::insertGetId($data);
        $this->log('添加司机',$data);
        return $this->success($id);
    }

    /**
     * 编辑
     * change 在 2021/10/25 15:37 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit(Request $request){
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' =>'required|integer',//企业类型主键id
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $messages = [
            'phone.required' => '手机号不能为空，请输入手机号',
            'phone.regex' => '手机号格式错误，请重试',
        ];

        $vali = Validator::make($data, [
            'phone' => 'required|regex:/^1[3456789][0-9]{9}$/',
        ], $messages);

        //参数错误响应
        if ($infos = $vali->errors()->first()) {
            return $this->fail('', $infos);
        }

        DriverModel::where('id',$data['id'])->update($data);
        $this->log('编辑司机',$data);
        return $this->success();
    }

    /**
     * 删除
     * change 在 2021/10/25 15:37 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function del(Request $request){
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' =>'required|integer',//企业类型主键id
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        $model = DriverModel::find($data['id']);
        $this->log('删除司机',$model);
        $model->delete();
        return $this->success();
    }
}
